package com.yygo.webapp.controller.loanrequest;

import com.yygo.model.LoanRequest;
import com.yygo.model.User;
import com.yygo.service.LoanRequestManager;
import com.yygo.service.UserManager;
import com.yygo.util.DateUtil;
import com.yygo.webapp.controller.SearchForm;
import com.yygo.webapp.helper.ExtendedPaginatedList;
import com.yygo.webapp.helper.PaginateListFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * Created by tiansha on 2015/10/6.
 */
@Controller
@RequestMapping("/policyRequests")
public class PolicyRequestsController extends BaseLoanRequestFormController {

    @Autowired
    private UserManager userManager;

    @Autowired
    private LoanRequestManager loanRequestManager;

    private PaginateListFactory paginateListFactory;
    @Autowired
    public void setPaginateListFactory(PaginateListFactory paginateListFactory) {
        this.paginateListFactory = paginateListFactory;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ModelAttribute
    public Model handleRequest(@RequestParam(required = false, value = "date") Date date,
                               @RequestParam(required = false, value = "salesman") String salesman, @RequestParam(required = false, value = "name") String name,
                               HttpServletRequest request, Principal principal)
            throws Exception {
        ExtendedPaginatedList paginatedList = paginateListFactory
                .getPaginatedListFromRequest(request);

        Model model = new ExtendedModelMap();

        if (date == null) {
            date = DateUtil.getDateBefore(10);
        }
        User loginUser = userManager.getUserByUsername(principal.getName());
        List<LoanRequest> list = loanRequestManager.getPolicyRequests(loginUser, date, name, salesman, paginatedList);

        // calculate policy fee
        calculatePolicyFee(list, loginUser.getBondCompany().isMasterProvider());

        model.addAttribute("loanRequestList", paginatedList);

        return model;
    }

    @ModelAttribute
    public SearchForm onForm(@RequestParam(required = false, value = "date") Date date,
                             @RequestParam(required = false, value = "salesman") String salesman, @RequestParam(required = false, value = "name") String name) {
        SearchForm form = new SearchForm();
        if (date == null) {
            date = DateUtil.getDateBefore(10);
        }
        form.setDate(date);
        form.setSalesman(salesman);
        form.setName(name);
        return form;
    }

    private void calculatePolicyFee(List<LoanRequest> loanRequests, boolean isMaster) {

        for (LoanRequest loanRequest : loanRequests) {
            float percentage = loanRequest.getDealer().getPremiumRate();
            float amount = loanRequest.getLoanAmount();
            BigDecimal totalFee = BigDecimal.valueOf(amount).multiply(BigDecimal.valueOf(1.1)).multiply(BigDecimal.valueOf(0.01575));
            if (isMaster) {
                float masterFee = totalFee.multiply(BigDecimal.valueOf(1.0-percentage)).floatValue();
                loanRequest.setPolicyFee(masterFee);
            } else {
                float masterFee = totalFee.multiply(BigDecimal.valueOf(percentage)).floatValue();
                loanRequest.setPolicyFee(masterFee);
            }
        }
    }

}
